<?php
/**
 * Created by PhpStorm.
 * User: geolo
 * Date: 2018/11/15
 * Time: 15:30
 */
defined('YII_RUN') or exit('Access Denied');
use yii\widgets\LinkPager;

$urlManager = Yii::$app->urlManager;
$this->title = '商品列表';
$this->params['active_nav_group'] = 2;
?>
<style>
    table {
        table-layout: fixed;
    }

    th {
        text-align: center;
        white-space: nowrap;
        text-overflow: ellipsis;
        overflow: hidden;
    }

    td {
        text-align: center;
    }

    .ellipsis {
        white-space: nowrap;
        text-overflow: ellipsis;
        overflow: hidden;
    }

    td.nowrap {
        white-space: nowrap;
        overflow: hidden;
    }

    .goods-pic {
        width: 3rem;
        height: 3rem;
        display: inline-block;
        background-color: #ddd;
        background-size: cover;
        background-position: center;
    }
    .pst{
        text-align: center;
        font-size: 16px;
        color:#00a0e9;
    }
    .bts{
       margin-left: 10px;
    }
</style>

<div class="panel mb-3">
    <div class="panel-header">基础面板
        <button class="bts">返回商品详情页</button>
    </div>

    <div class="panel-body">
        <?php
        $status = ['已下架', '已上架'];
        ?>
        <div class="mb-3 clearfix">
            <div class="float-left">
                <a href="javascript:void(0)" class="btn btn-secondary batch"data-content="确定选择"
                   data-url="<?= $urlManager->createUrl(['mch/content/choose']) ?>"
                   data-type="0">确定选择</a>
            </div>
            <div class="mb-3 clearfix">
                <div class="float-left">
                    <div class="dropdown float-right ml-2">
                        <button class="btn btn-secondary dropdown-toggle " type="button"   id="dropdownMenuButton"
                                data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                            <?= isset($_GET['cat']) ? $_GET['cat'] : '全部类型' ?>
                        </button>
                        <div class="dropdown-menu" aria-labelledby="dropdownMenuButton"
                             style="max-height: 200px;overflow-y: auto">
                            <a id="cho"  href="javascript:void(0)" class="dropdown-item" data-url="<?= $urlManager->createUrl(['mch/content/good']) ?>">全部类型</a>
                            <?php foreach ($cat_list as $index => $value): ?>
                                <a id="cho"  href="javascript:void(0)" class="dropdown-item "
                                    data-id = "<?= $value['cat_id']?>" data-url="<?= $urlManager->createUrl(array_merge(['mch/content/good'], $_GET, ['cat' => $value['name']])) ?>"><?= $value['name'] ?></a>
                            <?php endforeach; ?>
                        </div>
                    </div>
                </div>

            <div class="float-right">
                <form id='myform'method="post">
                    <input name="_csrf" type="hidden" id="_csrf" style="display:none" value="<?= Yii::$app -> request -> csrfToken ?>">
                    <input type="hidden" name="datainfo" id="datainfo" value="<?=$data?>">
                    <textarea style="display:none" name="content" id="content"><?=$content?></textarea>
                    <input type="hidden" name="id" id="id" value="<?= isset($_GET['content_id']) ? $_GET['content_id'] : $id?>">
                    <input type="hidden" name="goods_id" id="goods_id" value="<?=$goods_id?>">
                    <?php $_s = ['keyword'] ?>
                    <?php foreach ($_GET as $_gi => $_gv):if (in_array($_gi, $_s)) continue; ?>
                        <input type="hidden" name="<?= $_gi ?>" value="<?= $_gv ?>">
                    <?php endforeach; ?>
                    <div class="input-group">
                        <input class="form-control" placeholder="商品名/商品类型" name="keyword"
                               value="<?= isset($_GET['keyword']) ? trim($_GET['keyword']) : null ?>">
                        <span class="input-group-btn">
                    <button class="btn btn-primary">搜索</button>
                </span>
                    </div>
                </form>
            </div>
        </div>
        </div>
        <table class="table table-bordered bg-white">
            <thead>
            <tr>
                <!--                <th style="text-align: left"><span><input type="checkbox" class="goods-all"></span>&nbsp;&nbsp;ID</th>-->
                <th style="text-align: left">
                    <label class="checkbox-label">
<!--                        <input type="checkbox" class="goods-all">-->

                        <span class="label-icon"></span>
                        <span class="label-text">ID</span>
                    </label>
                </th>
                <th>商品类型</th>
                <th class="text-left">商品名称</th>
                <th>商品图片</th>
                <th>售价</th>
                <th>库存</th>


            </tr>
            </thead>
            <col style="width: 10%">
            <col style="width: 7%">
            <col style="width: 19%">
            <col style="width: 8%">
            <col style="width: 8%">
            <col style="width: 10%">

            <tbody>
            <?php foreach ($list as $index => $goods): ?>
                <tr>
                    <!--
                    <td class="nowrap" style="text-align: left" data-toggle="tooltip"
                        data-placement="bottom" title="<?=$goods->id?>">
                    <span>
                        <input data-num="<?= $goods->num ?>" type="checkbox"
                               class="goods-one"
                               value="<?= $goods->id ?>">
                    </span>&nbsp;&nbsp;<?= $goods->id ?>
                    </td>
                    -->
                    <td class="nowrap" style="text-align: left" data-toggle="tooltip"
                        data-placement="top" title="<?=$goods->id?>">
                        <label class="checkbox-label">
                            <input data-num="<?= $goods->num ?>" data-cat="<?=$goods->cat_id?>" type="checkbox"
                                   class="goods-one"
                                   value="<?= $goods->id ?>">
                            <span class="label-icon"></span>
                            <span class="label-text"><?= $goods->id ?></span>
                        </label>
                    </td>
                    <td class="nowrap"><?= $goods->cat->name ?></td>
                    <td class="text-left ellipsis"><?= $goods->name ?></td>
                    <td class="p-0" style="vertical-align: middle" hidden>
                        <div class="goods-pic" style="background-image: url(<?= $goods->getGoodsPic(0)->pic_url ?>)"></div>
                    </td>
                    <td class="p-0" style="vertical-align: middle">
                        <div class="goods-pic" style="background-image: url(<?= $goods->getGoodsCover() ?>)"></div>
                    </td>
                    <td class="nowrap text-danger"><?= $goods->price ?></td>
                    <td class="nowrap">
                        <?php if ($goods->use_attr): ?>
                            <a href="<?= $urlManager->createUrl(['mch/goods/goods-attr', 'id' => $goods->id]) ?>"><?= $goods->num ?></a>
                        <?php else: ?>
                            <a href="<?= $urlManager->createUrl(['mch/goods/goods-edit', 'id' => $goods->id]) ?>#step3"><?= $goods->num ?></a>
                        <?php endif; ?>
                    </td>
                </tr>
            <?php endforeach; ?>
            </tbody>

        </table>
        <nav aria-label="Page navigation example">
            <?php echo LinkPager::widget([
                'pagination' => $pagination,
                'prevPageLabel' => '上一页',
                'nextPageLabel' => '下一页',
                'firstPageLabel' => '首页',
                'lastPageLabel' => '尾页',
                'maxButtonCount' => 5,
                'options' => [
                    'class' => 'pagination',
                ],
                'prevPageCssClass' => 'page-item',
                'pageCssClass' => "page-item",
                'nextPageCssClass' => 'page-item',
                'firstPageCssClass' => 'page-item',
                'lastPageCssClass' => 'page-item',
                'linkOptions' => [
                    'class' => 'page-link',
                ],
                'disabledListItemSubTagOptions' => ['tag' => 'a', 'class' => 'page-link'],
            ])
            ?>
        </nav>
        <div>
            <a href="javascript:void(0)" class="btn btn-secondary batch"data-content="取消选择"
               data-url="<?= $urlManager->createUrl(['mch/content/con-del']) ?>"
               data-type="0">取消选择</a>
            <p class="pst">已选择的商品</p>
            <table class="table table-bordered bg-white">
                <thead>
                <tr>
                    <!--                <th style="text-align: left"><span><input type="checkbox" class="goods-all"></span>&nbsp;&nbsp;ID</th>-->
                    <th style="text-align: left">
                        <label class="checkbox-label">
                            <!--                        <input type="checkbox" class="goods-all">-->

                            <span class="label-icon"></span>
                            <span class="label-text">ID</span>
                        </label>
                    </th>
                    <th>商品类型</th>
                    <th class="text-left">商品名称</th>
                    <th>商品图片</th>
                    <th>售价</th>
                    <th>库存</th>


                </tr>
                </thead>
                <col style="width: 10%">
                <col style="width: 7%">
                <col style="width: 19%">
                <col style="width: 8%">
                <col style="width: 8%">
                <col style="width: 10%">

                <tbody>
                <?php foreach ($good_list as $index => $goods): ?>
                    <tr>
                        <!--
                    <td class="nowrap" style="text-align: left" data-toggle="tooltip"
                        data-placement="bottom" title="<?=$goods->id?>">
                    <span>
                        <input data-num="<?= $goods->num ?>" type="checkbox"
                               class="goods-one"
                               value="<?= $goods->id ?>">
                    </span>&nbsp;&nbsp;<?= $goods->id ?>
                    </td>
                    -->
                        <td class="nowrap" style="text-align: left" data-toggle="tooltip"
                            data-placement="top" title="<?=$goods->id?>">
                            <label class="checkbox-label">
                                <input  data-cat="<?=$goods->cat_id?>" type="checkbox"
                                       class="goods-one"
                                       value="<?= $goods->id ?>">
                                <span class="label-icon"></span>
                                <span class="label-text"><?= $goods->id ?></span>
                            </label>
                        </td>
                        <td class="nowrap"><?= $goods->cat->name ?></td>
                        <td class="text-left ellipsis"><?= $goods->name ?></td>
                        <td class="p-0" style="vertical-align: middle" hidden>
                            <div class="goods-pic" style="background-image: url(<?= $goods->getGoodsPic(0)->pic_url ?>)"></div>
                        </td>
                        <td class="p-0" style="vertical-align: middle">
                            <div class="goods-pic" style="background-image: url(<?= $goods->getGoodsCover() ?>)"></div>
                        </td>
                        <td class="nowrap text-danger"><?= $goods->price ?></td>
                        <td class="nowrap">
                            <?php if ($goods->use_attr): ?>
                                <a href="<?= $urlManager->createUrl(['mch/goods/goods-attr', 'id' => $goods->id]) ?>"><?= $goods->num ?></a>
                            <?php else: ?>
                                <a href="<?= $urlManager->createUrl(['mch/goods/goods-edit', 'id' => $goods->id]) ?>#step3"><?= $goods->num ?></a>
                            <?php endif; ?>
                        </td>
                    </tr>
                <?php endforeach; ?>
                </tbody>

            </table>
            <nav aria-label="Page navigation example">
                <?php echo LinkPager::widget([
                    'pagination' => $good_pagination,
                    'prevPageLabel' => '上一页',
                    'nextPageLabel' => '下一页',
                    'firstPageLabel' => '首页',
                    'lastPageLabel' => '尾页',
                    'maxButtonCount' => 5,
                    'options' => [
                        'class' => 'pagination',
                    ],
                    'prevPageCssClass' => 'page-item',
                    'pageCssClass' => "page-item",
                    'nextPageCssClass' => 'page-item',
                    'firstPageCssClass' => 'page-item',
                    'lastPageCssClass' => 'page-item',
                    'linkOptions' => [
                        'class' => 'page-link',
                    ],
                    'disabledListItemSubTagOptions' => ['tag' => 'a', 'class' => 'page-link'],
                ])
                ?>
            </nav>
        </div>
    </div>
</div>




<script>
        $(document).on('click', '.del', function () {
        if (confirm("是否删除？")) {
            $.ajax({
                url: $(this).attr('href'),
                type: 'get',
                dataType: 'json',
                success: function (res) {
                    alert(res.msg);
                    if (res.code == 0) {
                        window.location.reload();
                    }
                }
            });
        }
        return false;
    });

    function upDown(id, type) {
        var text = '';
        if (type == 'up') {
            text = "上架";
        } else {
            text = '下架';
        }

        var url = "<?= $urlManager->createUrl(['mch/goods/goods-up-down']) ?>";
        if (confirm("是否" + text + "？")) {
            $.ajax({
                url: url,
                type: 'get',
                dataType: 'json',
                data: {id: id, type: type},
                success: function (res) {
                    if (res.code == 0) {
                        window.location.reload();
                    }
                    if (res.code == 1) {
                        alert(res.msg);
                        if (res.return_url) {
                            location.href = res.return_url;
                        }
                    }
                }
            });
        }
        return false;
    }

    $(document).on('click', '.goods-all', function () {
        var checked = $(this).prop('checked');
        $('.goods-one').prop('checked', checked);
        if (checked) {
            $('.batch').addClass('is_use');
        } else {
            $('.batch').removeClass('is_use');
        }
    });
    $(document).on('click', '.goods-one', function () {
        var checked = $(this).prop('checked');
        var all = $('.goods-one');
        var is_all = true;//只要有一个没选中，全选按钮就不选中
        var is_use = false;//只要有一个选中，批量按妞就可以使用
        all.each(function (i) {
            if ($(all[i]).prop('checked')) {
                is_use = true;
            } else {
                is_all = false;
            }
        });
        if (is_all) {
            $('.goods-all').prop('checked', true);
        } else {
            $('.goods-all').prop('checked', false);
        }
        if (is_use) {
            $('.batch').addClass('is_use');
        } else {
            $('.batch').removeClass('is_use');
        }
    });
    $(document).on('click', '.batch', function () {
        var all = $('.goods-one');
        var is_all = true;//只要有一个没选中，全选按钮就不选中
        all.each(function (i) {
            if ($(all[i]).prop('checked')) {
                is_all = false;
            }
        });
        if (is_all) {
            $.myAlert({
                content: "请先勾选商品"
            });
        }
    });
    // 批量设置积分
    $(document).on('click', '.save-attr-btn', function () {
        var give = $('input[name^="integral[give]"]').val();
        var forehead = $('input[name^="integral[forehead]"]').val();
//        var more = $('input[name^="integral[more]"]').val();
        if ($('input[name^="integral[more]"]').is(':checked')) {
            var more = 1;
        } else {
            var more = '';
        }
        console.log(more);
        var all = $('.goods-one');
        var is_all = true;//只要有一个没选中，全选按钮就不选中
        all.each(function (i) {
            if ($(all[i]).prop('checked')) {
                is_all = false;
            }
        });
        if (is_all) {
            $.myAlert({
                content: "请先勾选商品"
            });
            return;
        }
        var a = $(this);
        var goods_group = [];
        all.each(function (i) {
            if ($(all[i]).prop('checked')) {
                var goods = {};
                goods_group.push($(all[i]).val());
            }
        });

        $.ajax({
            url: "<?= Yii::$app->urlManager->createUrl(['mch/goods/batch-integral']) ?>",
            type: 'get',
            dataType: 'json',
            data: {
                goods_group: goods_group,
                give: give,
                forehead: forehead,
                more: more,
            },
            success: function (res) {
                if (res.code == 0) {
                    window.location.reload();
                } else {
                    $('.modelError').text(res.msg);
                    $('.modelError').css('display', 'block');
                }
            },
//            complete: function () {
//                $.myLoadingHide();
//            }
        });


    });
    $(document).on('click', '.is_use', function () {
        var a = $(this);
        var goods_group = [];
        var all = $('.goods-one');
        var data = $("#datainfo").val();
        var content_s = $('#content').val();
        var id=$('#id').val();

        all.each(function (i) {
            if ($(all[i]).prop('checked')) {
                var goods = {};
                goods.id = $(all[i]).val();
                goods.num = $(all[i]).data('num');
                goods_group.push(goods);
            }
        });
        $.myConfirm({
            content: a.data('content'),
            confirm: function () {
                $.myLoading();
                $.ajax({
                    url: a.data('url'),
                    type: 'post',
                    dataType: 'json',
                    data: {
                        goods_group: goods_group,
                        id:id,
                        _csrf: _csrf,
                        type: a.data('type'),
                    },
                    success: function (res) {

                        if (res.code == 0){
                            $.myAlert({
                                content: "操作成功"
                            });
                            window.location.href='<?= Yii::$app->request->baseUrl ?>/index.php?r=mch/content/good&content_id='+res.id;
                        } else {
                            console.log(222);
                        }
                    },
                    complete: function () {
                        $.myLoadingHide();
                    }
                });
            }
        })
    });

    $(document).on('click', '#cho', function () {
        var a=$(this);
        var goods_group = [];
        var all = $('.goods-one');
        var data = $("#datainfo").val();
        var content_s = $('#content').val();
        var id=$('#id').val();

        all.each(function (i) {
            if ($(all[i]).prop('checked')) {
                var goods = {};
                goods.id = $(all[i]).val();
                goods.num = $(all[i]).data('num');
                goods_group.push(goods);
            }
        });
        if(goods_group.length>0) {
            if (confirm("是否已提交所选择的商品，否则页面刷新失去所选的？")) {
                var url = a.data('url');
                $("#myform").attr("action",url);

                $("#myform").submit();
            }
            return false;
        }else{
            var url = a.data('url');
            $("#myform").attr("action",url);

            $("#myform").submit();
        }


        })
        $(document).on('click', '.bts', function () {
            var newurl = '<?= $urlManager->createUrl(['mch/content/content-edit','id'=>$_GET['content_id']])?>';
            window.location.href=newurl;
        })
</script>
<script>
    $(document).ready(function () {
        var clipboard = new Clipboard('.copy');
        clipboard.on('success', function (e) {
            $.myAlert({
                title: '提示',
                content: '复制成功'
            });
        });
        clipboard.on('error', function (e) {
            $.myAlert({
                title: '提示',
                content: '复制失败，请手动复制。链接为：' + e.text
            });
        });
    })
</script>